<template>
  <MkDialogEdit ref="dialogRef" v-model:isEdit="ctx.isEdit" @submit="submit">
    <MkForm :model="ctx.entity" ref="formRef" formId="BasShuXueZhiZheng">
      <MkInput label='父节点' v-model='ctx.entity.pId' prop='pId' :isEdit='ctx.isEdit' />
      <MkInput label='名称' v-model='ctx.entity.mingCheng' prop='mingCheng' :isEdit='ctx.isEdit' />
      <MkInput label='血液大类' v-model='ctx.entity.daLei' prop='daLei' :isEdit='ctx.isEdit' />
      <MkInput label='用血类型' v-model='ctx.entity.yongXueLeiXing' prop='yongXueLeiXing' :isEdit='ctx.isEdit' />
      <MkInput label='科别(内科/外科)' v-model='ctx.entity.keBie' prop='keBie' :isEdit='ctx.isEdit' />
      <MkInput label='合理性判断函数' v-model='ctx.entity.functionBody' prop='functionBody' :isEdit='ctx.isEdit' />
      <MkInput label='排序' v-model='ctx.entity.orderNo' prop='orderNo' :isEdit='ctx.isEdit' />
      <MkInput label='备注' v-model='ctx.entity.remark' prop='remark' :isEdit='ctx.isEdit' />
      <MkIsEnabled label='是否启用' v-model='ctx.entity.isEnabled' prop='isEnabled' :isEdit='ctx.isEdit' />
    </MkForm>
  </MkDialogEdit>
</template>

<script lang='ts' setup>
import { BasShuXueZhiZhengApi, BasShuXueZhiZhengOutput } from '/@/api/bas/BasShuXueZhiZheng';
import MkForm from '/@/componentsMaike/forms/MkForm.vue';
import { isNull, clone } from '/@/utils';

const emits = defineEmits(['query'])
const ctx = reactive({
  isEdit: false,
  entity: {} as BasShuXueZhiZhengOutput,
});

const dialogRef = ref();
const formRef = ref<InstanceType<typeof MkForm>>();

/** 打开弹框 */
const open = async (row: BasShuXueZhiZhengOutput) => {
  dialogRef.value.show();
  if (isNull(row?.id)) {
    ctx.isEdit = true;
    ctx.entity = { pId: -1, isEnabled: true, orderNo: '500' } as BasShuXueZhiZhengOutput;
  } else {
    ctx.isEdit = false;
    ctx.entity = clone(row); // 不能直接引用
  }
};

const close = (isSubmit = false) => {
  dialogRef.value?.close();
  emits('query', isSubmit)
};

const submit = async () => {
  if (!ctx.isEdit) {
    close();
    return;
  }
  // 表单验证
  const validate = await formRef?.value?.validate();
  if (!validate) return;
  if (isNull(ctx.entity.id)) {
    await BasShuXueZhiZhengApi.add(ctx.entity);
  } else {
    await BasShuXueZhiZhengApi.update(ctx.entity);
  }
  close(true);
};

defineExpose({ open })
</script>
